$title macro_sam



* ------------------------------------------------------------------------------
* set options
* ------------------------------------------------------------------------------

* set default benchmark file
$if not set benchmark_file $set benchmark_file "data\default_aggregation.gdx"



* ------------------------------------------------------------------------------
* load model sets
* ------------------------------------------------------------------------------

* basic model sets
sets
  s                     sectors
  r                     regions
  h                     households
  trd                   trade markets
  f                     primary factors
  t                     time periods;

* set aliases
alias
  (s,ss,sss), (h,hh);

$gdxin %benchmark_file%
$load r s h trd f t



* ------------------------------------------------------------------------------
* load parameters
* ------------------------------------------------------------------------------

* benchmark data
parameters
    y0(r,s)         benchmark - output
    ld0(r,s)        benchmark - labor demand
    kd0(r,s)        benchmark - capital demand
    id0(r,s,ss)     benchmark - intermediate demand
    d0(r,s)         benchmark - domestic supply
    m0(r,s,trd)     benchmark - imports
    x0(r,s,trd)     benchmark - exports
    c0(r,h)         benchmark - consumption bundle
    cd0(r,s,h)      benchmark - consumer demand
    i0(r,s)         benchmark - investment demand
    invh0(r,h)      benchmark - household investment
    n0(r,h)         benchmark - households
    g0(r,s)         benchmark - government demand
    le0(r,h)        benchmark - household labor supply
    re0(r,h)        benchmark - capital rental income
    bopdef0(r,h)    benchmark - international balance of payments
    incadj0(r,h)    benchmark - government to household lump sum transfer
    ty0(r,s)        tax rate - production
    tl0(r)          tax rate - labor use
    tk0(r)          tax rate - capital use
    tc0(r)          tax rate - consumption;

* Load benchmark data
$load id0 ld0 kd0 cd0 x0 m0 i0 g0 re0 le0 invh0 n0 ty0 tl0 tc0 tk0

* calculate remaining variables from the original benchmark
y0(r,s)      = (sum(ss, id0(r,ss,s))+(1+tl0(r))*ld0(r,s)+(1+tk0(r))*kd0(r,s))/(1-ty0(r,s));
c0(r,h)      = sum(s, cd0(r,s,h))*(1+tc0(r));
bopdef0(r,h) = (sum(s, m0(r,s,"ftrd")-x0(r,s,"ftrd")))*c0(r,h)/sum(hh, c0(r,hh));
incadj0(r,h) = c0(r,h)+invh0(r,h)-re0(r,h)-le0(r,h)-bopdef0(r,h);



* ------------------------------------------------------------------------------
* check balance of national sam
* ------------------------------------------------------------------------------

set
  sam_accounts          macro sam accounts / activity, commodity, labor,
                                             capital, household, government,
                                             investment, national, foreign /;

alias (sam_accounts,aa);

* benchmark - government to household lump sum transfer
* set to the level that balances the household budget constraint
incadj0(r,h) = sum(s, (1+tc0(r))*cd0(r,s,h))+invh0(r,h)-re0(r,h)-le0(r,h)
               -bopdef0(r,h);

parameters
  macro_sam             macro sam;

* build macro sam
macro_sam("activity",   "commodity"  ) = sum((r,s), y0(r,s));
macro_sam("commodity",  "activity"   ) = sum((r,s,ss), id0(r,ss,s));
macro_sam("commodity",  "household"  ) = sum((r,s,h), cd0(r,s,h));
macro_sam("commodity",  "government" ) = sum((r,s), g0(r,s));
macro_sam("commodity",  "investment" ) = sum((r,s), i0(r,s));
macro_sam("commodity",  "national"   ) = sum((r,s), x0(r,s,"dtrd"));
macro_sam("commodity",  "foreign"    ) = sum((r,s), x0(r,s,"ftrd"));
macro_sam("labor",      "activity"   ) = sum((r,s), ld0(r,s));
macro_sam("capital",    "activity"   ) = sum((r,s), kd0(r,s));
macro_sam("household",  "labor"      ) = sum((r,h), le0(r,h));
macro_sam("household",  "capital"    ) = sum((r,h), re0(r,h));
macro_sam("household",  "government" ) = sum((r,h), incadj0(r,h));
macro_sam("household",  "foreign"    ) = sum((r,h), bopdef0(r,h));
macro_sam("government", "activity"   ) = sum((r,s),  ty0(r,s)*y0(r,s)
                                                    +tk0(r)*kd0(r,s)
                                                    +tl0(r)*ld0(r,s));
macro_sam("government", "household"  ) = sum((r,s,h), tc0(r)*cd0(r,s,h));
macro_sam("investment", "household"  ) = sum((r,h), invh0(r,h));
macro_sam("national",   "commodity"  ) = sum((r,s), m0(r,s,"dtrd"));
macro_sam("foreign",    "commodity"  ) = sum((r,s), m0(r,s,"ftrd"));

* compute the row and colum totals and check the differences
macro_sam(sam_accounts,"total") = sum(aa, macro_sam(sam_accounts,aa));
macro_sam("total",sam_accounts) = sum(aa, macro_sam(aa,sam_accounts));
macro_sam("r-c diff",sam_accounts) =  macro_sam(sam_accounts,"total")
                                     -macro_sam("total",sam_accounts);

display macro_sam;

parameters
  macro_stats(*,*)      basic statistics for the macro economy;

macro_stats("consumption exp","value") = sum((r,h,s), cd0(r,s,h));
macro_stats("investment exp","value")  = sum((r,s),i0(r,s));
macro_stats("government exp","value")  = sum((r,s), g0(r,s));
macro_stats("exports","value")         = sum((r,s), x0(r,s,"ftrd"));
macro_stats("imports","value")         = sum((r,s), m0(r,s,"ftrd"));
macro_stats("gdp by exp","value")      =  macro_stats("consumption exp","value")
                                         +macro_stats("investment exp","value")
                                         +macro_stats("government exp","value")
                                         +macro_stats("exports","value")
                                         -macro_stats("imports","value");
macro_stats("labor va","value")        = sum((r,s), (1+tl0(r))*ld0(r,s));
macro_stats("capital va","value")      = sum((r,s), (1+tk0(r))*kd0(r,s));
macro_stats("prod tax","value")        = sum((r,s), ty0(r,s)*y0(r,s));
macro_stats("gdp by va","value")       =  macro_stats("labor va","value")
                                         +macro_stats("capital va","value")
                                         +macro_stats("prod tax","value");
macro_stats("households","value")      = sum((r,h), n0(r,h));

display macro_stats;
